Java JavaScript Python C# C C++ Go Kotlin PHP Swift R Ruby TypeScript Scala SQL Perl rust VisualBasic Matlab Julia

Queue Interface → Queue in Java

Queue Interface

Queue in Java

Queue and Deque interface in Java

The Java Collections Framework doesn't actually include a specific Queue interface. However, it provides two important related interfaces that define queue-like behavior: java.util.Queue: This interface outlines the core operations expected of a queue data structure. It extends the Collection interface and focuses on insertion and removal operations. java.util.Deque (Double-ended Queue): This interface extends Queue and adds functionalities for accessing and removing elements from both ends of the queue (front and back).

Characteristics of Queue

FIFO (First-In-First-Out) order: Elements are processed in the order they were added. The element that was added first (to the back) will be the first one to be removed (from the front). Unordered: The elements themselves are not maintained in any specific order within the queue. Common Implementations for Queue-like behavior: java.util.LinkedList: Although LinkedList implements the List interface, it can also be used as a queue due to its efficient insertion at the end (add(E element)) and removal from the front (poll()) operations. Methods of the Queue Interface:add(E element): Adds the specified element to the back of the queue. ⮚offer(E element): Attempts to add the specified element to the back of the queue. Returns true if successful, false if not (e.g., due to capacity restrictions). ⮚poll(): Retrieves and removes the head (first) element from the queue, or null if the queue is empty. ⮚remove(): Retrieves and removes the head element from the queue. Throws a NoSuchElementException if the queue is empty. ⮚peek(): Retrieves, but does not remove, the head element from the queue, or null if the queue is empty. ⮚isEmpty(): Checks if the queue is empty.

Using LinkedList as a Queue

Example of using linkedlist as a queue in Java import java.util.LinkedList; public class Main { public static void main(String[] args) { // Create a LinkedList as a queue LinkedList<String> names = new LinkedList<>(); names.add("Name 1"); names.add("Name 2"); names.add("Name 3"); // Process names in FIFO order while (!names.isEmpty()) { String nextName = names.poll(); System.out.println("Processing Name: " + nextName); } } }

Output

Processing Name: Name 1 Processing Name: Name 2 Processing Name: Name 3
In this example, LinkedList is used as a queue. Tasks are added to the back (add) and processed from the front (poll), demonstrating the FIFO behavior.

Deque Interface:

The Deque interface extends Queue and provides additional methods for accessing and removing elements from both ends: ⮚addFirst(E element): Adds the element at the front of the deque. ⮚addLast(E element) (same as add in Queue): Adds the element to the back of the deque. ⮚offerFirst(E element): Attempts to add the element at the front of the deque. ⮚offerLast(E element): (same as offer in Queue):** Attempts to add the element to the back of the deque. ⮚pollFirst(): Retrieves and removes the first element from the deque, or null if empty. ⮚pollLast(): Retrieves and removes the last element from the deque, or null if empty. ⮚peekFirst(): Retrieves, but does not remove, the first element from the deque, or null if empty. ⮚peekLast(): Retrieves, but does not remove, the last element from the deque, or null if empty. By understanding queues and dequeues and their implementations like LinkedList, you can effectively manage collections where elements need to be processed in a specific order (FIFO) in your Java programs.

Tutorials